package cc.idoone.spring_boot_hello.cors.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * CORS 跨域请求全局配置
 *
 * @author hemj
 * @version 2017/9/22
 */
@Configuration
public class CorsConfigurationBean {

	@Bean
	public WebMvcConfigurer corsConfigurer() {
		return new WebMvcConfigurerAdapter() {
			@Override
			public void addCorsMappings(CorsRegistry registry) {
				registry.addMapping("/cors/**")
						.allowedOrigins("http://localhost:8080")
						.allowedMethods("GET", "HEAD", "POST", "OPTIONS")
						.maxAge(3600);
				;
			}
		};
	}
}
